package com.feifan.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.feifan.api.annotation.LogType;
import com.feifan.api.annotation.SystemLog;
import com.feifan.api.annotation.UserLoginToken;
import com.feifan.api.model.OmsOrderReturnReason;
import com.feifan.api.service.IOmsOrderReturnReasonService;
import com.feifan.common.CommonPage;
import com.feifan.common.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: FinalProject
 * @author: Xiaofan
 * @createTime: 2021-08-06 15:31
 * @description: Functions of this class is
 **/

@RestController
@Api(tags = "OmsOrderReturnReasonController",description = "退货原因管理")
@RequestMapping("/returnReason")
@CrossOrigin
public class OmsOrderReturnReasonController {

    //dubbo做法
    @Reference(
            version = "1.0.0",
            interfaceName = "com.feifan.api.service.IOmsOrderReturnReasonService",
            interfaceClass = IOmsOrderReturnReasonService.class,
            timeout = 170000
    )
    private IOmsOrderReturnReasonService service;

    @ApiOperation(value = "添加退货原因")
    @ResponseBody
    @SystemLog(description = "添加退货原因",type = LogType.ORDER_ADD_RETURN_REASON)
    @UserLoginToken//即必须登录才可以使用该功能
    @PostMapping(value = "/create")
    public CommonResult create(@RequestBody OmsOrderReturnReason returnReason){
        CommonResult commonResult;
        try {
            service.create(returnReason);
            commonResult=CommonResult.success(true);
        }
        catch (Exception e){
            commonResult=CommonResult.failed();
            e.printStackTrace();
        }
        return commonResult;
    }
    @ApiOperation(value = "批量删除退货原因")
    @ResponseBody
    @PostMapping(value = "/delete")
    @UserLoginToken
    @SystemLog(description = "批量删除退货原因",type = LogType.ORDER_DELETE_RETURN_REASON)
    public CommonResult delete(@RequestParam(value = "ids")List<Long> ids){
        Integer sum=service.delete(ids);
        if(sum>=0){
            return CommonResult.success(sum);
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "更新退货原因")
    @ResponseBody
    @PostMapping(value = "/update/{id}")
    @UserLoginToken
    @SystemLog(description = "更新退货原因",type = LogType.ORDER_UPDATE_RETURN_REASON)
    public CommonResult update(@PathVariable Long id,@RequestBody OmsOrderReturnReason returnReason){
        CommonResult commonResult;
        try {
            service.update(id,returnReason);
            commonResult=CommonResult.success(true);
        }
        catch (Exception e){
            commonResult= CommonResult.failed();
        }
        return commonResult;
    }

    @ApiOperation(value = "更新退货原因状态")
    @ResponseBody
    @PostMapping(value = "/update/status")//地址这里不能用{status}不然获取不到
    @UserLoginToken
    @SystemLog(description = "更新退货原因状态",type = LogType.ORDER_UPDATE_RETURN_REASON_STATUS)
    public CommonResult updateStatus(@RequestParam(value = "status") Integer status,@RequestParam(value = "ids") Long id){
        CommonResult commonResult;
        try {
            service.updateStatue(id,status);
            commonResult=CommonResult.success(true);
        }
        catch (Exception e){
            commonResult= CommonResult.failed();
        }
        return commonResult;
    }

    @ApiOperation(value = "获取单个退货原因")
    @ResponseBody
    @GetMapping(value = "/{id}")
    @UserLoginToken
    @SystemLog(description = "获取单个退货原因",type = LogType.ORDER_RETURN_REASON)
    public CommonResult getItem(@PathVariable Long id){
        return CommonResult.success(service.getOmsOrderReturnReason(id));
    }

    @ApiOperation(value = "分页查询所有退货原因")
    @ResponseBody
    @SystemLog(description = "分页查询所有退货原因",type = LogType.ORDER_RETURN_REASON_LIST)
    @UserLoginToken//即必须登录才可以使用该功能
    @GetMapping("/list")
    public CommonResult<CommonPage<OmsOrderReturnReason>> list(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                                               @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){
//        System.out.println(service.list(pageNum, pageSize));
        return CommonResult.success(service.list(pageNum, pageSize));
    }
}